package com.yijiupi.plugin.test.TextOnlyTreeStructureProvider;

import com.intellij.ide.projectView.TreeStructureProvider;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TextOnlyTreeStructureProvider implements TreeStructureProvider {
	/**
	 * Allows a plugin to modify the list of children displayed for the specified node in the
	 * project view.
	 *
	 * @param parent   the parent node.
	 * @param children the list of child nodes according to the default project structure.
	 *                 Elements of the collection are of type {@link ProjectViewNode}.
	 * @param settings the current project view settings.
	 * @return the modified collection of child nodes, or {@code children} if no modifications
	 * are required.
	 */
	@Override
	public @NotNull Collection<AbstractTreeNode<?>> modify(@NotNull AbstractTreeNode<?> parent, @NotNull Collection<AbstractTreeNode<?>> children, ViewSettings settings) {
		ArrayList<AbstractTreeNode<?>> nodes = new ArrayList<>();
		for (AbstractTreeNode<?> child : children) {
			if (child instanceof PsiFileNode) {
				VirtualFile file = ((PsiFileNode) child).getVirtualFile();
				if (file != null && !file.isDirectory() && !(file.getFileType() instanceof PlainTextFileType)) {
					continue;
				}
			}
			nodes.add(child);
		}
		return nodes;
	}

	/**
	 * Returns a user data object of the specified type for the specified selection in the
	 * project view.
	 *
	 * @param selected the list of nodes currently selected in the project view.
	 * @param dataId   the identifier of the requested data object (for example, as defined in
	 *                 {@link PlatformDataKeys})
	 * @return the data object, or null if no data object can be returned by this provider.
	 * @see DataProvider
	 */
	@Override
	public @Nullable Object getData(@NotNull Collection<AbstractTreeNode<?>> selected, @NotNull String dataId) {
		Iterator<AbstractTreeNode<?>> iterable= selected.iterator();
		return selected;
	}
}
